package connexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnexionOracle {
	
	private String path = "jdbc:oracle:thin:@miage03.dmiage.u-paris10.fr:1521:MIAGE";
	private String user = "bahouarn";
	private String password = "apprentis2010pw";
	private Connection connection;
	
	public ConnexionOracle(){
		
	}
	
	public ConnexionOracle(String path, String user, String password) {
		this.path = path;
		this.user = user;
		this.password = password;
	}
	
	public Connection connect() {
		try {
			// Chargement du driver ODBC
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// Connexion à la base
			connection = DriverManager.getConnection(path, user,
					password);
		} catch (ClassNotFoundException e) {
			System.out.println("Problème avec le driver ODBC");
			return null;
		} catch (SQLException e) {
			System.out.println("Impossible de se connecter à la base");
			return null;
		}
		return connection;
	}
	
	/**
	 * Déconnexion de la base
	 * 
	 * @return : true si la déconnexion est réussite, false sinon
	 */
	public boolean disconnect() {
		try {
			connection.close();
			return true;
		} catch (SQLException e) {
			return false;
		}
	}
	
	/**
	 * Envoi d'une requête de séletion
	 * 
	 * @param : sql
	 * @return : result
	 */
	public ResultSet SQLSelect(String sql) throws SQLException {
		Statement statement = null;
		ResultSet result = null;
		try {
			statement = connection.createStatement();
			result = statement.executeQuery(sql);
			return result;
		} catch (SQLException e) {
			result.close();
			statement.close();
			return null;
		}
	}
	

	/**
	 * Envoi d'une requête de mise à jour (insert, update, delete)
	 * 
	 * @param : sql
	 */
	public void SQLUpdate(String sql) throws SQLException {
		Statement statement = null;
		try {
			statement = connection.createStatement();
			statement.executeUpdate(sql);
		} catch (SQLException e) {
			statement.close();
		}
	}


}
